import { getMenuList } from '@/http/api.js'
import { dealRouterData } from '@/utils/computedRoutes.js'

const state = {
	addRoutes: [],
	hasgetRoutes: false,
}
const mutations = {
	CHANGE_ADDROUTES(state, arr) {
		state.addRoutes = arr
		state.hasgetRoutes = true
	},
	CLEAR_ADDROUTES(state) {
		state.addRoutes = []
		state.hasgetRoutes = false
	},
}
const actions = {
	ADD_ROUTES({ commit }) {
		return new Promise(reslove => {
			getMenuList({ whetherEnable: 0 }).then(res => {
				let data = res?.data
				if (data?.data && Array.isArray(data.data) && data.data.length > 0) {
					commit('CHANGE_ADDROUTES', data.data)
					// console.log(dealRouterData(data.data))
					const res = dealRouterData(data.data)
					reslove(res)
				} else {
					commit('CHANGE_ADDROUTES', [])
					reslove([])
				}
			})
		})
	},
}
export default {
	namespaced: true,
	state,
	mutations,
	actions,
}
